package com.wisemoresystem.seal.base.plugin.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;

public class PageInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest req,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse resp,
			Object arg2, ModelAndView mv) throws Exception {
		if (arg2.getClass() == HandlerMethod.class) {
			HandlerMethod hm = (HandlerMethod) arg2;
			ProjBaseAction baseAction = (ProjBaseAction) hm.getBean();
			if(ChkTools.isNotNull(mv)){
				mv.addObject("page", baseAction.page);
			}
		}

	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse resp, Object arg2) throws Exception {
		
		if (arg2.getClass() == HandlerMethod.class) {
			HandlerMethod hm = (HandlerMethod) arg2;
			ProjBaseAction baseAction = (ProjBaseAction) hm.getBean();
			baseAction.page = new Page();
			
			int pageNum = baseAction.page.getPageNum();
			String pageNoStr = request.getParameter("page.pageNum");
			if (ChkTools.isNotNull(pageNoStr)) {
				pageNum = Integer.parseInt(pageNoStr);
			}
			baseAction.page.setPageNum(pageNum);
		}

		return true;
	}
}
